package com.wsc.wisdomchina.DaoUtils;

import android.content.Context;
import android.util.Log;

import com.wsc.wisdomchina.DaoManager.DaoManager;
import com.wsc.wisdomchina.Entity.IndexNameSort;
import com.wsc.wisdomchina.Entity.IndexNameSortDao;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * author: xw
 * created on: 2017/10/26 14:35
 * description:
 */
public class IndexNameSortDaoUtils {
    private static final String TAG = IndexNameSortDaoUtils.class.getSimpleName();
    private DaoManager mManager;

    public IndexNameSortDaoUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成indexNameSort记录的插入，如果表未创建，先创建indexNameSort表
     *
     * @param indexNameSort
     * @return
     */
    public boolean insertIndexNameSort(IndexNameSort indexNameSort) {
        boolean flag = false;
        flag = mManager.getDaoSession().getIndexNameSortDao().insert(indexNameSort) == -1 ? false : true;
        Log.i(TAG, "insert Meizi :" + flag + "-->" + indexNameSort.toString());
        return flag;
    }


    /**
     * 插入多条数据，在子线程操作
     *
     * @param indexNameSortList
     * @return
     */
    public boolean insertMultIndexNameSort(final List<IndexNameSort> indexNameSortList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (IndexNameSort meizi : indexNameSortList) {
                        mManager.getDaoSession().insertOrReplace(meizi);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param meizi
     * @return
     */
    public boolean updateIndexNameSort(IndexNameSort meizi) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(meizi);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param IndexNameSort
     * @return
     */
    public boolean deleteIndexNameSort(IndexNameSort meizi) {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(meizi);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(IndexNameSort.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<IndexNameSort> queryAllIndexNameSort() {
        return mManager.getDaoSession().loadAll(IndexNameSort.class);
    }

    /**
     * 根据主键id查询记录
     *
     * @param key
     * @return
     */
    public IndexNameSort queryMeiziById(long key) {
        return mManager.getDaoSession().load(IndexNameSort.class, key);
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<IndexNameSort> queryMeiziByNativeSql(String sql, String[] conditions) {
        return mManager.getDaoSession().queryRaw(IndexNameSort.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     *
     * @return
     */
    public List<IndexNameSort> queryIndexNameSortByQueryBuilder(String uid) {
        QueryBuilder<IndexNameSort> queryBuilder = mManager.getDaoSession().queryBuilder(IndexNameSort.class);
        return queryBuilder.where(IndexNameSortDao.Properties.Uid.eq(uid)).list();
    }

}